﻿/// <reference path="../../knockout-2.1.0.js" />
/// <reference path="../../knockout.mapping-latest.js" />
/// <reference path="../shared/utils.js" />
/// <reference path="../../jquery-1.7.2-vsdoc.js" />
/// <reference path="../../knockout.wrap.js" />
function ExamenModel(data) {
    this.Id = ko.observable(data.Id);
    this.IdCita = ko.observable(data.IdCita);
    this.Tipo = ko.observable(data.Tipo);
    this.FechaCreacion = ko.observable(data.FechaCreacion);

}

function ExamenViewModel(idIndividuo) {
    var self = this;
    self.Examenes = ko.observableArray([]);
    $.getJSON("/Reporte/ListaExamenes/" + idIndividuo, function (response) {
        if (response.lista)
            self.Examenes(response.lista);
    });
    self.PageSize = ko.observable(5);
    self.PageIndex = ko.observable(0);
    self.PreviousPage = function () { self.PageIndex(self.PageIndex() - 1); };
    self.NextPage = function () { self.PageIndex(self.PageIndex() + 1); };
    self.PagedRows = ko.dependentObservable(function () {
        var size = self.PageSize();
        var start = self.PageIndex() * size;
        return self.Examenes.slice(start, size + start);
    });
    self.MaxPageIndex = ko.dependentObservable(function () {
        return Math.ceil(self.Examenes().length / self.PageSize()) - 1;
    });
   
}

function IndividuoDetalle(data) {
    var self = this;
    self.Load = function (info) { ko.mapping.fromJS(info, {}, this); };
    if (data)
        self.Load(data);
}

function IndividuoDetalleViewModel(idIndividuo) {
    var self = this;
    self.ExamenesVM = ko.observable();
    self.Load = function (id) {
        var ss = $.param({ id: id });
        var url = "/Registro/ObtenerIndividuoDetalle?" + ss;
        $.getJSON(url, function (data) {
            if (data) {
                self.IndividuoDetalle(new IndividuoDetalle(data.schema));
                self.ExamenesVM(new ExamenViewModel(idIndividuo));
            }
        });
    };

    self.IndividuoDetalle = ko.observable();

    self.NombreCompleto = ko.computed(function () {
        var current = self.IndividuoDetalle();
        if (current != null)
            return current.Nombres() + " " + current.PrimerApellido();
    }, self);

    self.Load(idIndividuo);
};

function BuscarUsuarioViewModel() {
    var self = this;
    self.Results = ko.observableArray([]);
    self.NoIdentificacionSearch = ko.observable('');
    self.Apellido = ko.observable('');

    /*Paging*/
    self.PageSize = ko.observable(25);
    self.PageIndex = ko.observable(0);
    self.PreviousPage = function () { self.PageIndex(self.PageIndex() - 1); };
    self.NextPage = function () { self.PageIndex(self.PageIndex() + 1); };
    self.MaxPageIndex = ko.computed(function () {
        return Math.ceil(self.Results().length / self.PageSize()) - 1;
    });
    self.PagedResults = ko.computed(function () {
        var size = self.PageSize();
        var start = self.PageIndex() * size;
        return self.Results.slice(start, size + start);
    });
    /*end Paging*/

    self.Mostar = function (selected) {
        location.href = '../Reporte/Index?id=' + selected.IndividuoId();
    };

    self.Buscar = function () {
        var ss = $.param({ id: self.NoIdentificacionSearch(), apellido: self.Apellido() });
        var url = "../Registro/ObtenerIndividuos?" + ss;
        $.getJSON(url, function (response) {
            if (response.data)
                self.Load(response.data);
        });
    };

    self.Load = function (data) {
        self.Results(ko.mapping.fromJS(data)());
    };

}


function startApp(id) {
    if (id) {
        ko.applyBindings(new IndividuoDetalleViewModel(id), document.getElementById("ExamenesBinding"));
    } else
        ko.applyBindings(new BuscarUsuarioViewModel(), document.getElementById("BuscarUsuarioContext"));
}
